package demo.controller.adm;

import demo.dao.ClzDao;
import demo.dao.CourseDao;
import demo.dao.FileDao;
import demo.dao.UserDao;
import demo.entity.Clz;
import demo.entity.Course;
import demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/courseadm")
public class CourseAdmController {
    @Autowired(required = false)
    UserDao userDao;
    @Autowired(required = false)
    FileDao fileDao;
    @Autowired(required = false)
    CourseDao courseDao;
    @GetMapping("/dels/{clzno}")
    public String dels(@PathVariable("clzno") String clzno, Model model){
        int del = courseDao.del(clzno);
        model.addAttribute("actions", "删除成功");
        return "forward:/courseadm/show2/1";
    }
    @RequestMapping("/show2/{curline}")
    public String show2(@PathVariable("curline") Integer curline, Model model, HttpSession session){
        if (curline!=null){
            Course course=new Course();
            model.addAttribute("course",course);
            model.addAttribute("action","添加");
            model.addAttribute("go2","add2s");
        }else curline=1;
        int cou = courseDao.findCou();

        int lines=cou/6;
        if (curline>lines){
            curline=lines;
        }
        model.addAttribute("curline",curline);
        model.addAttribute("lines",lines);
        List<Course> all=courseDao.findpage((curline - 1) * 6, 6);
        model.addAttribute("list",all);
        return "/adm/course";
    }
    @RequestMapping("curline")
    public String curline(Model model,Integer curline){
        if (curline==1)return "forward:/courseadm/show2/1";
        curline=curline-1;
        model.addAttribute("curline",curline);
        return "forward:/courseadm/show2/"+curline;
    }
    @RequestMapping("lines")
    public String lines(Model model,Integer curline){
        curline=curline+1;
        model.addAttribute("curline",curline);
        return "forward:/courseadm/show2/"+curline;
    }
    @GetMapping("/adds")
    public String adds(Model model){
        Course course=new Course();
        model.addAttribute("action","添加");
        model.addAttribute("go2","add2s");
        model.addAttribute("course",course);
        return "forward:/courseadm/show2";
    }
    @GetMapping("/ups/{clzno}")
    public String ups(@PathVariable("clzno") String clzno,Model model){
        Course byClzno = courseDao.findByClzno(clzno);
        model.addAttribute("action","更新");
        model.addAttribute("go2","up2s");
        model.addAttribute("course",byClzno);
        return "forward:/courseadm/show2";
    }
    @PostMapping("/add2s")
    public String add2s(Course course,Model model){
        int add=courseDao.add(course);
        model.addAttribute("action","更新");
        model.addAttribute("actions", "添加成功");
        return "forward:/courseadm/show2/1";
    }
    @PostMapping("/up2s")
    public String up2s(Course course,Model model){
        int add=courseDao.up(course);
        model.addAttribute("action","更新");
        model.addAttribute("actions", "更新成功");
        return "forward:/courseadm/show2/1";
    }
    @RequestMapping("/show2")
    public String show(Model model,HttpSession session){
        User dl =(User) session.getAttribute("DL");
        if (dl==null)return "forward:/home/go2login";
        int cou = courseDao.findCou();
        int lines=cou/6;
        model.addAttribute("curline",1);
        model.addAttribute("lines",lines);
        List<Course> all=courseDao.findpage((1 - 1) * 6, 6);
        model.addAttribute("list",all);
        return "/adm/course";
    }
}
